<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;

class CreateProjectEvents extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'project:events {project_name} {events_name}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '创建API集群项目事件: project:events 项目名 事件名';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    protected function getStub()
    {
        return __DIR__ . '/stubs/project/events.stub';
    }

    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace . '\Events';
    }


    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $args = $this->arguments();

        $namespaceExplode = explode('/', str_replace(['/', '//'], '/', $args['events_name']));
        $fileExplode = null;
        if (count($namespaceExplode) > 1) {
            $classname = ucfirst(array_pop($namespaceExplode)) . 'Events';
            foreach ($namespaceExplode as $k => $v) {
                $namespaceExplode[$k] = ucfirst($v);
            }
            $namespace = 'Project\\' . ucfirst($args['project_name']) . '\\Events\\' . join('\\', $namespaceExplode);
            $fileExplode = join('/', $namespaceExplode) . '/';
        } else {
            // NameSpace
            $namespace = 'Project\\' . ucfirst($args['project_name']) . '\\Events';
            // Class name
            $classname = ucfirst($args['events_name']) . 'Events';
        }

        // File name
        $file_name = ucfirst($classname . '.php');

        // File path
        $logic_file = app_path() .
            '/Project/' .
            ucfirst($args['project_name']) .
            '/Events/' .
            $fileExplode .
            $file_name;

        // File dir
        $logic_path = dirname($logic_file);

        // 首次创建项目
        if (!is_dir(app_path('project/' . ucfirst($args['project_name']) . '/Events'))) {
            @mkdir(app_path('project/' . ucfirst($args['project_name'])), 0755, true);
            @mkdir(app_path('project/' . ucfirst($args['project_name']) . '/Events'), 0755, true);
        }

        //获取模板,替换变量
        $template = file_get_contents(dirname(__FILE__) . '/stubs/project/events.stub');
        $source = str_replace('{namespace}', $namespace, $template);
        $source = str_replace('{classname}', $classname, $source);
        $source = str_replace('{snake_classname}', Str::snake($classname), $source);

        //是否已存在相同文件
        if (file_exists($logic_file)) {
            $this->error('文件已存在');
            exit;
        }

        //创建
        if (file_exists($logic_path) === false) {
            if (mkdir($logic_path, 0755, true) === false) {
                $this->error('目录 <' . $logic_path . '> 没有写入权限');
                exit;
            }
        }

        //写入
        if (!file_put_contents($logic_file, $source)) {
            $this->error('创建失败！');
            exit;
        }

        $this->info('创建成功！');
    }
}
